<template>
  <div class="">
    <div class="search">
      <el-input
        placeholder="请输入内容"
        v-model="input3"
        class="input-with-select"
        clearable
        @input="onInput"
      >
        <el-button slot="append" icon="el-icon-search"></el-button>
      </el-input>
      <el-button type="primary" class="buttons">添加用户</el-button>
    </div>
    <el-table
      ref="singleTable"
      :data="tableData.slice((currpage - 1) * pagesize, currpage * pagesize)"
      highlight-current-row
      style="width: 100%"
    >          
      <el-table-column type="index"> </el-table-column>
      <el-table-column property="username" label="姓名"> </el-table-column>
      <el-table-column property="email" label="邮箱"> </el-table-column>
      <el-table-column property="mobile" label="电话"> </el-table-column>
      <el-table-column property="role_name" label="角色"> </el-table-column>
      <el-table-column label="日期">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{ scope.row.newdata }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态">
        <template slot-scope="scope">
          <div>
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <div class="edit">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              circle
              @click="onedit(scope.row)"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="Delete(scope.row.id)"
              circle
            ></el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-star-off"
              circle
            ></el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 2, 3, 4]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
    >
    </el-pagination>
  </div>
</template>
<script>
export default {
  props: {},
  data() {
    return {
      input3: "",
      pagesize: 5,
      currpage: 1,
    };
  },
  //事件处理器
  methods: {
    //搜索事件
    onInput() {
      //   console.log(this.input3);
      //延迟400毫秒搜索
      window.setTimeout(() => {
        this.$store.dispatch("Search", this.input3);
      }, 400);
    },
    //一页多少个
    handleSizeChange(page) {
      this.pagesize = page;
    },
    //当前第几页
    handleCurrentChange(page) {
      this.currpage = page;
    },
    //点击弹删除
    Delete(id) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        //确认删除
        .then(() => {
          this.$store.dispatch("Delete", id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //点击弹出编辑
    onedit(el) {
      this.$prompt("用户名", "编辑用户", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          this.$message({
            type: "success",
            message: "用户名: " + value,
          });
          el.username = value;
          this.$store.dispatch("onedit", el);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
    //点击弹出添加
    AddUser() {
      this.$prompt("用户名", "添加用户", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(({ value }) => {
          this.$message({
            type: "success",
            message: "用户名: " + value,
          });
          //   console.log(value);
          this.$store.dispatch("AddUser", value);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消输入",
          });
        });
    },
  },
  // 计算属性
  computed: {
    //监听vuex中的数据
    tableData() {
      //多次扩展，实现数据明显变化
      var temp = [
        ...this.$store.state.tableData,
        ...this.$store.state.tableData,
      ];
      return temp;
    },
  },
  //侦听器
  watch: {},
  //生命周期 - 创建完成（访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.$message({
      message: "获取角色列表成功",
      type: "success",
    });
  },
};
</script>
<style scoped>
/* @import url(); 引入css类 */
.input-with-select {
  width: 70%;
}
.search {
  width: 40%;
}
.buttons {
  margin-left: 0.625rem;
}
</style>
